package com.ccx.service.impl;

import com.ccx.mapper.EbProductCategoryMapper;
import com.ccx.mapper.EbProductMapper;
import com.ccx.model.EbProduct;
import com.ccx.model.EbProductCategory;
import com.ccx.service.ProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private EbProductMapper ebProductMapper;  //调用mapper层的方法与属性

    @Autowired
    private EbProductCategoryMapper ebProductCategoryMapper;

    @Override
    public int save(EbProduct ebProduct){
        //1.小类
        int smallId = ebProduct.getEpcChildId();
        //2.根据小类获取小类信息
        EbProductCategory ebProduct1 = ebProductCategoryMapper.selectByPrimaryKey(smallId);
        //3.获取其小类的父类
        Integer epcParentId = ebProduct1.getEpcParentId();
        //4.父类ID绑定到添加商品
        ebProduct.setEpcId(epcParentId);

        return ebProductMapper.insertSelective(ebProduct);
    }

    public List<EbProduct> list(int page,int pageSize){

        PageHelper.startPage(page,pageSize);
        List<EbProduct>ebProducts = ebProductMapper.selectByExample(null);
        return ebProducts;

    }

    @Override
    public EbProduct selectOne(Integer epId) {
        return ebProductMapper.selectByPrimaryKey(epId);
    }

    @Override
    public int delect(int epId) {
        return ebProductMapper.deleteByPrimaryKey(epId);
    }

    @Override
    public List<EbProduct> selectSuccess(int eoId, String eoUserName, int page, int pageSize) {
        return null;
    }

    @Override
    public int update(EbProduct ebProduct) {
        int smallId = ebProduct.getEpcChildId();
        EbProductCategory categoryObj = ebProductCategoryMapper.selectByPrimaryKey(smallId);
        Integer epcParentId = categoryObj.getEpcParentId();
        ebProduct.setEpcId(epcParentId);
        return ebProductMapper.updateByPrimaryKeySelective(ebProduct);

    }

    @Override
    public int insert(EbProduct ebProduct) {
        int smallId = ebProduct.getEpcChildId();
        EbProductCategory categoryObj = ebProductCategoryMapper.selectByPrimaryKey(smallId);
        Integer epcParentId = categoryObj.getEpcParentId();
        ebProduct.setEpcId(epcParentId);
        return ebProductMapper.insertSelective(ebProduct);
    }

    @Override
    public EbProduct selectbyId(int epId) {
        return  ebProductMapper.selectByPrimaryKey(epId);
    }
}

